package com.jd.webapp.portal.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.jd.common.convention.TTResult;
import com.jd.common.util.CookieUtils;
import com.jd.common.util.HttpClientUtil;
import com.jd.common.util.JsonUtils;
import com.jd.goods.pojo.Goods;
import com.jd.webapp.portal.pojo.CartItem;



@Service
public class CartServiceImpl implements CartService {
	private static final String TT_CART = "TT_CART";
	@Value("${REST_SERVER_URL}")
	private String REST_SERVER_URL;

	@Override
	public void addCart(long itemid, int num, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		List<CartItem> carts = getCarts(request);

		// 定义查找标识find
		boolean find = false;
		// 遍历购物车,查找是否存在该商品
		for (CartItem item : carts) {
			if (item.getId().longValue() == itemid) {
				item.setNum(item.getNum() + num);
				find = true;
				break;
			}
		}

		if (!find) {
			// 获取商品的信息
			String json = HttpClientUtil.doGet(REST_SERVER_URL + "/item/base/"
					+ itemid);
			TTResult result = TTResult.formatToPojo(json, Goods.class);
			Goods item = (Goods) result.getData();
			// 添加一个新的购物车商品
			CartItem cart = new CartItem();
			cart.setId(item.getId());
			cart.setTitle(item.getTitle());
			cart.setImage(item.getImage());
			cart.setNum(num);
			cart.setPrice(item.getPrice());
			carts.add(cart);
		}
		String cartsjson = JsonUtils.objectToJson(carts);
		// 添加cookie
		CookieUtils.setCookie(request, response, TT_CART, cartsjson, true);
	}

	private List<CartItem> getCarts(HttpServletRequest request) {
		List<CartItem> carts = new ArrayList<CartItem>();
		// 先查询当前的购物车
		String cookieValue = CookieUtils.getCookieValue(request, TT_CART, true);
		if (!StringUtils.isEmpty(cookieValue)) {
			carts = JsonUtils.jsonToList(cookieValue, CartItem.class);
		}
		return carts;
	}

	@Override
	public List<CartItem> getList(HttpServletRequest request) throws Exception {
		return getCarts(request);
	}

	@Override
	public void deleteCart(long itemid, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<CartItem> carts = getCarts(request);
		for (CartItem cart : carts) {
			if (cart.getId().longValue() == itemid) {
				carts.remove(cart);
				break;
			}
		}
		// 更新cookie
		String cartsjson = JsonUtils.objectToJson(carts);
		// 添加cookie
		CookieUtils.setCookie(request, response, TT_CART, cartsjson, true);
	}

	@Override
	public void updateCart(long itemid, int num, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<CartItem> carts = getCarts(request);

		// 遍历购物车,查找是否存在该商品
		for (CartItem item : carts) {
			if (item.getId().longValue() == itemid) {
				item.setNum(num);
				break;
			}
		}
		// 更新cookie
		String cartsjson = JsonUtils.objectToJson(carts);
		// 添加cookie
		CookieUtils.setCookie(request, response, TT_CART, cartsjson, true);
	}
}
